package utils;

import Message.Message;

import java.io.*;
import java.sql.Timestamp;


/**
 * 该类原来是针对Mesage类进行文件读写操作，现在停用
 * */
public class ReadWriteFile {
    private String LogPath;

    public ReadWriteFile(){
        File directory = new File("");//设定为当前文件夹
        String LogPath = "";
        try{
            LogPath = directory.getCanonicalPath() + "/";//获取标准的路径
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.LogPath = LogPath + "Log/";
    }

    public String getLogPath() {
        return LogPath;
    }

    public  void writeInFile(String fileName, String content) {
        String filePathtemp = this.getLogPath() + fileName;
        File file = new File(filePathtemp);
        if (!file.exists()) {
            try {
                file.createNewFile(); //如果文件不存在则创建文件
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Writer writer = null;
        StringBuilder outputString = new StringBuilder();
        try {
            outputString.append(content + "\r\n");
            writer = new FileWriter(file, true); // true表示追加
            writer.write(outputString.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
    }

    //该方法从文件中读取该用户与某一用户的聊天记录，MessageFromSomeOneList.get(i)中的message.userIdInDatabaseFrom默认为该用户id，
    //userIdInDatabaseTo则默认为该用户的聊天对象的用户ID，参数userid为该用户的Uid
//    public MessageFromSomeOneList readFile(String filePath, String userid) throws IOException {
//        String filePathtemp = this.getLogPath() + filePath;
//        File file = new File(filePathtemp);
//        InputStreamReader read = null;// 考虑到编码格式
//        try {
//            read = new InputStreamReader(new FileInputStream(file), "utf-8");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//        BufferedReader bufferedReader = new BufferedReader(read);
//        String lineTxt = null;
//        MessageFromSomeOneList messageFromSomeOneList = new MessageFromSomeOneList();
//        while ((lineTxt = bufferedReader.readLine()) != null) {
//            String[] splitstr=lineTxt.split("\t");
//            String userIdInDatabaseFrom = splitstr[0];
//            String userIdInDatabaseTo = splitstr[1];
//            Timestamp time = Timestamp.valueOf(splitstr[2]);
//            int messageType = Integer.valueOf(splitstr[3]).intValue();
//            String messageInfo = splitstr[4];
//            Message message = new Message();
//            if(userid.equals(userIdInDatabaseFrom)){
//                message.setUserIdInDatabaseFrom(userIdInDatabaseFrom);
//                message.setUserIdInDatabaseTo(userIdInDatabaseTo);
//            }else{
//                message.setUserIdInDatabaseFrom(userIdInDatabaseTo);
//                message.setUserIdInDatabaseTo(userIdInDatabaseFrom);
//            }
//            message.setMessageType(messageType);
//            message.setTimeByTimestamp(time);
//            message.setMessageInfo(messageInfo);
//            messageFromSomeOneList.add(message);
//        }
//        read.close();
//        return messageFromSomeOneList;
//    }

    public static void main(String[] args) throws IOException {
        ReadWriteFile readWriteFile = new ReadWriteFile();
        System.out.println(readWriteFile.getLogPath());
        String filePath = "0_888888889_888888881.txt";
//        MessageFromSomeOneList messageFromSomeOneList = readWriteFile.readFile(filePath, "888888889");
//        System.out.println(JaxbUtil.convertToXml(messageFromSomeOneList));
    }
}